package com.panfeng.xcloud.boss.provider.member.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.panfeng.xcloud.boss.provider.member.service.IUserVoiceCodeVerifyService;
import com.panfeng.xcloud.boss.provider.member.web.vo.request.VoiceCodeReqVO;
import com.panfeng.xcloud.boss.provider.member.web.vo.response.VoiceCodeRespVO;
import com.panfeng.xcloud.common.core.utils.ConvertUtils;
import com.panfeng.xcloud.common.core.web.vo.ResponseVO;
import com.panfeng.xcloud.common.security.annotation.MethodValidate;
import com.panfeng.xcloud.common.security.annotation.NotNeedAccess;
import com.panfeng.xcloud.common.security.web.support.BaseController;
import com.panfeng.xcloud.component.sms.model.VoiceCodeReqDTO;
import com.panfeng.xcloud.component.sms.model.VoiceCodeRespDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 语音验证模块
 *
 * @author xiaobo
 * @version 1.0
 * @since 2019-08-08
 */
@ApiIgnore
@RestController
@RequestMapping("user/voice")
@Api(tags = {"语音验证模块"})
@Slf4j
public class UserVoiceCodeController extends BaseController {

    @Autowired
    private IUserVoiceCodeVerifyService iUserVoiceCodeVerifyService;

    /**
     * 语音验证
     *
     * @param voiceCodeReqVO
     * @param bindingResult
     * @return
     */
    @MethodValidate
    @NotNeedAccess
    @PostMapping(value = "/voiceVerify", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "语音验证(肖博)", httpMethod = "POST", notes = "语音验证(肖博)")
    public ResponseVO<VoiceCodeRespVO> voiceVerify(@RequestBody @Valid VoiceCodeReqVO voiceCodeReqVO, HttpServletRequest request, BindingResult bindingResult) {
        logger.info(">>>> 当前voiceVerify请求参数：{} <<<", (null != voiceCodeReqVO ? JSONObject.toJSONString(voiceCodeReqVO) : ""));
        VoiceCodeReqDTO voiceCodeReqDTO = ConvertUtils.convert(voiceCodeReqVO, VoiceCodeReqDTO.class);
        VoiceCodeRespDTO voiceCodeRespDTO = iUserVoiceCodeVerifyService.voiceVerification(voiceCodeReqDTO,request);
        VoiceCodeRespVO voiceCodeRespVO = ConvertUtils.convert(voiceCodeRespDTO, VoiceCodeRespVO.class);
        logger.info(">>>> 当前voiceVerify响应内容：{} <<<", (null != voiceCodeRespVO ? JSONObject.toJSONString(voiceCodeRespVO) : ""));
        return null != voiceCodeRespDTO && voiceCodeRespDTO.getIsOk() ? ResponseVO.with(voiceCodeRespVO): ResponseVO.error();
    }

}
